<?php
/**
 * 获取患者巡查记录
 * @authors Hex (hex@zpdchina.com)
 * @date    2017-03-09 17:41:33
 * @version $Id$
 */

class XunchajiluDataModel extends BaseModel {

    /**
     * 返回给终端的可执行sql语句的字段名称数组
     * @var [type]
     */
    public $_field_arr = ['id','zhuyuan_id','xuncha_time','xuncha_hushi_id','xuncha_hushi_name','modify_time'];
    public $_field_detail_arr = ['id','xuncha_jilu_id','xuncha_item_code','xuncha_item_name','xuncha_item_type','modify_time'];

    public function getXunchaData($user_department,$data,$modify_time = '')
    {
        $field_str = implode(',', $this->_field_arr);
        $query = DB::select($field_str)
            ->from('zhuyuan_xuncha_jilu')
            ->where('yiyuan_id', '=', $this->yiyuan_id);
        if($modify_time)
            $query->where('modify_time', '>=', $modify_time);
        if (!empty($data))
            $query->where('id', 'NOT IN', $data);
        $zhuayun_bingren = CommonModel::getZhuyuanIds($user_department,$this->yiyuan_id);
        if($zhuayun_bingren)
            $query->where('zhuyuan_id', 'IN', $zhuayun_bingren);
        $zhuyuan_xuncha = $query ->execute()->as_array();

        return $zhuyuan_xuncha;
    }

    public function getXunchaDetailData($user_department,$data,$modify_time = '')
    {
        $query = DB::select('a.*')
            ->from(array('zhuyuan_xuncha_jilu_detail', 'a'))
            ->join(array('zhuyuan_xuncha_jilu', 'b'), 'LEFT')
            ->on('a.xuncha_jilu_id', '=', 'b.id')
            ->on('a.yiyuan_id', '=', 'b.yiyuan_id')
            ->join(array('zhuyuan_basic_info', 'c'), 'LEFT')
            ->on('b.zhuyuan_id', '=', 'c.zhuyuan_id')
            ->on('a.yiyuan_id', '=', 'c.yiyuan_id')
            ->where('c.zhuyuan_bingqu', '=', $user_department)
            ->where('c.zhuangtai', '=', '住院中')
            ->where('b.xuncha_time', '>=', DB::expr("DATE_SUB(CURDATE(), INTERVAL 2 DAY)"))
            ->where('a.yiyuan_id', '=', $this->yiyuan_id);
        if($modify_time)
            $query->where('a.modify_time', '>=', $modify_time);
        if (!empty($data))
            $query->where('a.id', 'NOT IN', $data);
        $zhuyuan_xuncha_detail = $query->execute()->as_array();

        return $zhuyuan_xuncha_detail;
    }

}
